const DB = require('../../utils/db');

module.exports = {
	list: async (keywords) => {
		let [list] = await DB.query(`select *,date_format(create_time,'%Y-%m-%d %H:%i:%s') 
                  createTime,date_format(update_time,'%Y-%m-%d %H:%i:%s') updateTime from t_music where title like '%${keywords}%'`);
		return list;
	},
	add: async (title, author, url, pic, lrc) => {
		let [list] = await DB.query(`select * from t_music where title='${title}'`);
		if (list.length) throw new Error('音乐名称重复');
            
		await DB.query('insert into t_music(title,author,url,pic,lrc) values(?,?,?,?,?)', [title, author, url, pic, lrc]);
	},
	edit: async (title, author, url, pic, lrc, id) => {
		await DB.query('update t_music set title=?,author=?,url=?,pic=?,lrc=? where id=?', [title, author, url, pic, lrc, id]);
	},
	detail: async (id) => {
		let [list] = await DB.query('select * from t_music where id=?', [id]);
		if (!list.length) throw new Error('音乐ID有误');
		return list[0];
	},
	del: async (id, res) => {
		await DB.query('delete from t_music where id=?', [id]);
	}
};
